package com.ruoyi.web.controller.demo;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.DemoCustomer;
import com.ruoyi.demo.service.base.DemoCustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 客户信息Controller
 * 
 * @author zhangshu
 * @date 2021-01-26
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/demo/customer" )
public class DemoCustomerController extends BaseController {

    private final DemoCustomerService iDemoCustomerService;

    /**
     * 查询客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('demo:customer:list')")
    @GetMapping("/list")
    @DataScope(deptAlias = "d",userAlias = "u")
    public TableDataInfo list(DemoCustomer demoCustomer) {
        startPage();
        List<DemoCustomer> list = iDemoCustomerService.queryList(demoCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('demo:customer:export')" )
    @Log(title = "客户信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(DemoCustomer demoCustomer) {
        List<DemoCustomer> list = iDemoCustomerService.queryList(demoCustomer);
        ExcelUtil<DemoCustomer> util = new ExcelUtil<DemoCustomer>(DemoCustomer.class);
        return util.exportExcel(list, "customer" );
    }

    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('demo:customer:query')" )
    @GetMapping(value = "/{customerId}" )
    public AjaxResult getInfo(@PathVariable("customerId" ) Long customerId) {
        return AjaxResult.success(iDemoCustomerService.getById(customerId));
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('demo:customer:add')" )
    @Log(title = "客户信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DemoCustomer demoCustomer) {
        return toAjax(iDemoCustomerService.save(demoCustomer) ? 1 : 0);
    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('demo:customer:edit')" )
    @Log(title = "客户信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DemoCustomer demoCustomer) {
        return toAjax(iDemoCustomerService.updateById(demoCustomer) ? 1 : 0);
    }

    /**
     * 删除客户信息
     */
    @PreAuthorize("@ss.hasPermi('demo:customer:remove')" )
    @Log(title = "客户信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerIds}" )
    public AjaxResult remove(@PathVariable Long[] customerIds) {
        return toAjax(iDemoCustomerService.removeByIds(Arrays.asList(customerIds)) ? 1 : 0);
    }
}
